/*
*
* ScaleUI
*
* This class implements a scale for intepreting neighbourhood shading
*
*/
class ScaleUI {
  
  // -------------------------------------
  // Attributes
  // -------------------------------------
  ControlP5 controlp5;
  PApplet parent;
  DModeManager dModeMan;
  float origX, origY, wid, hei;    // dimensions for the scale
  int divs = 30;                   // number of scale divisions
  float val;
  Textlabel maxLabel, minLabel, noDataLabel, displayTitle, modeLabel;
  String ma = "";
  String mi = "";
  
  // -------------------------------------
  // Constructor
  // -------------------------------------
  ScaleUI(ControlP5 cp5, PApplet p55, float x, float y, float w, float h) {
    
    // set attributes
    
    controlp5 = cp5;
    parent = p55;
    origX = x;
    origY = y;
    wid = w;
    hei = h;
    
    // create scale labels
    
    maxLabel = controlp5.addTextlabel("maxLabel", ma, int(origX + wid + 4), int(origY + 2));
    maxLabel.setColorValue(0);
    maxLabel.hide(); // drawn during draw loop
    minLabel = controlp5.addTextlabel("minLabel", mi, int(origX + wid + 4), int(origY + hei - 8));
    minLabel.setColorValue(0);
    minLabel.hide(); // drawn during draw loop
    minLabel.setColorBackground(0);
    noDataLabel = controlp5.addTextlabel("noDataLabel", "no data", int(origX + wid + 4), int(origY + hei + 5));
    noDataLabel.setColorValue(0);
    noDataLabel.hide(); // drawn during draw loop
    noDataLabel.setColorBackground(0);
    
    // create drawing mode labels
    displayTitle = controlp5.addTextlabel("displayTitle", "displaying:", int(origX + wid + 50), int(origY + 2));
    displayTitle.setColorValue(0);
    displayTitle.hide(); // drawn during draw loop
    displayTitle.setColorBackground(0);
    modeLabel = controlp5.addTextlabel("modeLabel", "", int(origX + wid + 50), int(origY + 15));
    modeLabel.setColorValue(0);
    modeLabel.hide(); // drawn during draw loop
    modeLabel.setColorBackground(0);
    
  } // constructor
  
  // -------------------------------------
  // Draw
  // draws scale
  // -------------------------------------
  void draw(DModeManager dmm, boolean pdf) {
    
    // retrieve current drawing mode
    DMode dMode = dmm.getDMode();
    
//    // draw "frame" rectangle; if pdf, add stroke, fill white, and narrow
//    if(pdf) {
//      fill(255);
//      stroke(150);
//      rect((origX - 4), (origY - 4), 190, (hei + 20));
//    } else {
//      noStroke();
//      fill(50, 50);
//      rect((origX - 4), (origY - 4), 225, (hei + 20));
//    }
    
    // draw "frame" rectangle, but only if not pdf mode
    if(!pdf) {
      noStroke();
      fill(50, 50);
      rect((origX - 4), (origY - 4), 225, (hei + 20));
    }
    
    color col;
    noStroke();
    
    // check to make sure drawing mode needs scale drawn
    if(!(dMode.hasMetric)) {
      // metric doesn't need scale drawn
        
      // hide scale labels if mode doesn't require a scale
      minLabel.hide();
      maxLabel.hide();
      noDataLabel.hide();
      
      // show display title (ie label that says "displaying")
      displayTitle.show();
      
      // refresh mode label
      modeLabel.setValue(dMode.name);
      modeLabel.show();
      
    } else {
      
      // draw scale
      
      // pdf drawing mode has slightly different position and number of scale divisions
      if(pdf) {
        for(int i = 0; i < 10; i++) {
          
          // draw coloured portion
          val = dMode.getMax() - (i + 0.5) * (dMode.getMax() - dMode.getMin()) / 10;
          col = dMode.getColour(val);
          fill(col);
          rect(origX + 10, (origY + 9 + hei / 10 * i), wid, (hei / 10));
          
          // draw "no data" square
          fill(noDataFil);
          rect(origX + 10, (origY + 33 + hei - 19), wid, 8);
          
        } // for      
      } else {
        for(int i = 0; i < divs; i++) {
          
          // draw coloured portion
          val = dMode.getMax() - (i + 0.5) * (dMode.getMax() - dMode.getMin()) / divs;
          col = dMode.getColour(val);
          fill(col);
          rect(origX, (origY + hei / divs * i), wid, (hei / divs));
          
          // draw "no data" square
          fill(noDataFil);
          rect(origX, (origY + hei + 5), wid, 8);
          
        } // for
      } // if
      
      
      // draw labels
      
      // update values
      mi = nf(dMode.getMin(), 3, 1);
      ma = nf(dMode.getMax(), 3, 1);
      minLabel.setValue(mi);
      maxLabel.setValue(ma);
      modeLabel.setValue(dMode.name);
      
      // get name for metric category
      // this is a hack, but just doing it for the print
      String raceName;
      int ind = dmm.dMode;
      
      if(ind <= 3) {
        raceName = "Election metric";
      } else if(ind <=10) {
        raceName = "Mayor";
      } else if(ind <=13) {
        raceName = "Council: Ward 1";
      } else if(ind <=19) {
        raceName = "Council: Ward 2";  
      } else if(ind <=28) {
        raceName = "Council: Ward 3";
      } else if(ind <=33) {
        raceName = "Council: Ward 4";
      } else if(ind <=37) {
        raceName = "Council: Ward 5";
      } else if(ind <=45) {
        raceName = "Council: Ward 6";
      } else if(ind <=50) {
        raceName = "Council: Ward 7";
      } else if(ind <=55) {
        raceName = "Council: Ward 8";
      } else if(ind <=59) {
        raceName = "Council: Ward 9";
      } else if(ind <=61) {
        raceName = "Council: Ward 10";
      } else if(ind <=67) {
        raceName = "Council: Ward 11";
      } else if(ind <=71) {
        raceName = "Council: Ward 12";
      } else if(ind <=73) {
        raceName = "Catholic School Board: Ward 71";
      } else if(ind <=76) {
        raceName = "Catholic School Board: Ward 72";
      } else if(ind <=79) {
        raceName = "Catholic School Board: Ward 73";
      } else if(ind <=81) {
        raceName = "Catholic School Board: Ward 74";
      } else if(ind <=84) {
        raceName = "Catholic School Board: Ward 75";
      } else if(ind <=88) {
        raceName = "Catholic School Board: Ward 76";
      } else if(ind <=91) {
        raceName = "Catholic School Board: Ward 77";
      } else if(ind <=94) {
        raceName = "Public School Board: Ward A";
      } else if(ind <=99) {
        raceName = "Public School Board: Ward B";
      } else if(ind <=102) {
        raceName = "Public School Board: Ward C";
      } else if(ind <=103) {
        raceName = "Public School Board: Ward D";
      } else if(ind <=106) {
        raceName = "Public School Board: Ward E";
      } else if(ind <=109) {
        raceName = "Public School Board: Ward F";
      } else if(ind <=111) {
        raceName = "Public School Board: Ward G";
      } else if(ind <=112) {
        raceName = "Public School Board: Ward H";
      } else if(ind <=115) {
        raceName = "Public School Board: Ward I";
      } else {
        raceName = "ERROR";
      }
      
      // summary metric
      // this is for writing a "summary metric," like a candidates overall race vote share
      // again, a total hack. This code generated with calcVotes.rb, which is in svn
      String sumOut = "";
      
      if(ind == 1) {
        sumOut = "Overall: 33.4";
      } else if(ind == 2) {
        sumOut = "Average: " + nf(dMode.ave, 3, 1);
      } else if(ind == 3) {
        sumOut = "Average: " + nf(dMode.ave, 3, 1);
      } else if(ind == 4) {
        sumOut = "Overall: 10.4";
      } else if(ind == 5) {
        sumOut = "Overall: 29.5";
      } else if(ind == 6) {
        sumOut = "Overall: 1.2";
      } else if(ind == 7) {
        sumOut = "Overall: 2.3";
      } else if(ind == 8) {
        sumOut = "Overall: 0.6";
      } else if(ind == 9) {
        sumOut = "Overall: 0.6";
      } else if(ind == 10) {
        sumOut = "Overall: 54.9";
      } else if(ind == 11) {
        sumOut = "Overall: 33.1";
      } else if(ind == 12) {
        sumOut = "Overall: 10.3";
      } else if(ind == 13) {
        sumOut = "Overall: 54.5";
      } else if(ind == 14) {
        sumOut = "Overall: 3.3";
      } else if(ind == 15) {
        sumOut = "Overall: 39.1";
      } else if(ind == 16) {
        sumOut = "Overall: 42.5";
      } else if(ind == 17) {
        sumOut = "Overall: 4.8";
      } else if(ind == 18) {
        sumOut = "Overall: 6.8";
      } else if(ind == 19) {
        sumOut = "Overall: 1.4";
      } else if(ind == 20) {
        sumOut = "Overall: 10.1";
      } else if(ind == 21) {
        sumOut = "Overall: 23.9";
      } else if(ind == 22) {
        sumOut = "Overall: 1.0";
      } else if(ind == 23) {
        sumOut = "Overall: 27.4";
      } else if(ind == 24) {
        sumOut = "Overall: 5.5";
      } else if(ind == 25) {
        sumOut = "Overall: 16.2";
      } else if(ind == 26) {
        sumOut = "Overall: 2.8";
      } else if(ind == 27) {
        sumOut = "Overall: 6.4";
      } else if(ind == 28) {
        sumOut = "Overall: 4.4";
      } else if(ind == 29) {
        sumOut = "Overall: 5.6";
      } else if(ind == 30) {
        sumOut = "Overall: 38.1";
      } else if(ind == 31) {
        sumOut = "Overall: 45.5";
      } else if(ind == 32) {
        sumOut = "Overall: 5.9";
      } else if(ind == 33) {
        sumOut = "Overall: 3.3";
      } else if(ind == 34) {
        sumOut = "Overall: 19.0";
      } else if(ind == 35) {
        sumOut = "Overall: 2.4";
      } else if(ind == 36) {
        sumOut = "Overall: 5.9";
      } else if(ind == 37) {
        sumOut = "Overall: 70.5";
      } else if(ind == 38) {
        sumOut = "Overall: 9.5";
      } else if(ind == 39) {
        sumOut = "Overall: 42.8";
      } else if(ind == 40) {
        sumOut = "Overall: 3.7";
      } else if(ind == 41) {
        sumOut = "Overall: 15.2";
      } else if(ind == 42) {
        sumOut = "Overall: 19.2";
      } else if(ind == 43) {
        sumOut = "Overall: 1.9";
      } else if(ind == 44) {
        sumOut = "Overall: 1.1";
      } else if(ind == 45) {
        sumOut = "Overall: 2.5";
      } else if(ind == 46) {
        sumOut = "Overall: 47.1";
      } else if(ind == 47) {
        sumOut = "Overall: 35.0";
      } else if(ind == 48) {
        sumOut = "Overall: 3.3";
      } else if(ind == 49) {
        sumOut = "Overall: 4.4";
      } else if(ind == 50) {
        sumOut = "Overall: 8.0";
      } else if(ind == 51) {
        sumOut = "Overall: 3.8";
      } else if(ind == 52) {
        sumOut = "Overall: 46.5";
      } else if(ind == 53) {
        sumOut = "Overall: 25.1";
      } else if(ind == 54) {
        sumOut = "Overall: 12.3";
      } else if(ind == 55) {
        sumOut = "Overall: 9.7";
      } else if(ind == 56) {
        sumOut = "Overall: 67.8";
      } else if(ind == 57) {
        sumOut = "Overall: 6.8";
      } else if(ind == 58) {
        sumOut = "Overall: 8.9";
      } else if(ind == 59) {
        sumOut = "Overall: 13.8";
      } else if(ind == 60) {
        sumOut = "Overall: 74.6";
      } else if(ind == 61) {
        sumOut = "Overall: 23.1";
      } else if(ind == 62) {
        sumOut = "Overall: 8.4";
      } else if(ind == 63) {
        sumOut = "Overall: 43.3";
      } else if(ind == 64) {
        sumOut = "Overall: 5.3";
      } else if(ind == 65) {
        sumOut = "Overall: 2.6";
      } else if(ind == 66) {
        sumOut = "Overall: 28.0";
      } else if(ind == 67) {
        sumOut = "Overall: 10.5";
      } else if(ind == 68) {
        sumOut = "Overall: 2.6";
      } else if(ind == 69) {
        sumOut = "Overall: 7.2";
      } else if(ind == 70) {
        sumOut = "Overall: 25.9";
      } else if(ind == 71) {
        sumOut = "Overall: 63.4";
      } else if(ind == 72) {
        sumOut = "Overall: 63.6";
      } else if(ind == 73) {
        sumOut = "Overall: 30.8";
      } else if(ind == 74) {
        sumOut = "Overall: 27.4";
      } else if(ind == 75) {
        sumOut = "Overall: 42.5";
      } else if(ind == 76) {
        sumOut = "Overall: 24.6";
      } else if(ind == 77) {
        sumOut = "Overall: 14.2";
      } else if(ind == 78) {
        sumOut = "Overall: 25.6";
      } else if(ind == 79) {
        sumOut = "Overall: 55.2";
      } else if(ind == 80) {
        sumOut = "Overall: 69.2";
      } else if(ind == 81) {
        sumOut = "Overall: 27.1";
      } else if(ind == 82) {
        sumOut = "Overall: 37.0";
      } else if(ind == 83) {
        sumOut = "Overall: 23.4";
      } else if(ind == 84) {
        sumOut = "Overall: 30.2";
      } else if(ind == 85) {
        sumOut = "Overall: 47.2";
      } else if(ind == 86) {
        sumOut = "Overall: 12.6";
      } else if(ind == 87) {
        sumOut = "Overall: 25.5";
      } else if(ind == 88) {
        sumOut = "Overall: 8.8";
      } else if(ind == 89) {
        sumOut = "Overall: 28.7";
      } else if(ind == 90) {
        sumOut = "Overall: 32.2";
      } else if(ind == 91) {
        sumOut = "Overall: 34.0";
      } else if(ind == 92) {
        sumOut = "Overall: 60.1";
      } else if(ind == 93) {
        sumOut = "Overall: 8.7";
      } else if(ind == 94) {
        sumOut = "Overall: 20.0";
      } else if(ind == 95) {
        sumOut = "Overall: 27.4";
      } else if(ind == 96) {
        sumOut = "Overall: 12.6";
      } else if(ind == 97) {
        sumOut = "Overall: 6.3";
      } else if(ind == 98) {
        sumOut = "Overall: 8.8";
      } else if(ind == 99) {
        sumOut = "Overall: 38.5";
      } else if(ind == 100) {
        sumOut = "Overall: 21.4";
      } else if(ind == 101) {
        sumOut = "Overall: 23.0";
      } else if(ind == 102) {
        sumOut = "Overall: 41.3";
      } else if(ind == 103) {
        sumOut = "Candidate acclaimed";
      } else if(ind == 104) {
        sumOut = "Overall: 29.0";
      } else if(ind == 105) {
        sumOut = "Overall: 34.5";
      } else if(ind == 106) {
        sumOut = "Overall: 28.0";
      } else if(ind == 107) {
        sumOut = "Overall: 47.0";
      } else if(ind == 108) {
        sumOut = "Overall: 10.8";
      } else if(ind == 109) {
        sumOut = "Overall: 29.9";
      } else if(ind == 110) {
        sumOut = "Overall: 65.4";
      } else if(ind == 111) {
        sumOut = "Overall: 28.3";
      } else if(ind == 112) {
        sumOut = "Candidate acclaimed";
      } else if(ind == 113) {
        sumOut = "Overall: 38.4";
      } else if(ind == 114) {
        sumOut = "Overall: 37.5";
      } else if(ind == 115) {
        sumOut = "Overall: 16.4";
      }

      
      // write labels using Processing font functions if writing to pdf; show P5 labels if not
      if(pdf) {
        fill(130);
        textFont(pdfExportFontSmall);
        text(ma, int(origX + wid + 14), int(origY + 16));
        text(mi, int(origX + wid + 14), int(origY + hei + 8));
        text("no data", int(origX + wid + 14), int(origY + hei + 22));
        text(dMode.name, int(origX + 10), int(origY - 8));
        text(raceName, int(origX + 10), int(origY - 20));
        text(sumOut, int(origX + 10), int(origY + 4));
      } else {
        // show labels
        minLabel.show();
        maxLabel.show();
        noDataLabel.show();
        displayTitle.show();
        modeLabel.show();
      }
    } // if
  } // draw method
  
} // Scale UI class
